// 包含n个reducer函数 根据老state和指定的action返回一个新的state
import {combineReducers} from 'redux'
import {AUTH_SUCCESS, ERROR_MSG, RESET_USER, RECEIVE_USER} from './action-types'
import {getRedirectTo} from "../utils";

const initUser = {
    username:'',
    type:'',
    msg:'', // 存放错误提示信息
    redirectTo:''   // 指定需要自动重定向的路由路径
};
// 产生user状态的reducer
function user(state = initUser, action) {
    switch (action.type) {
        case AUTH_SUCCESS: // data是user
            const {type, header} = action.data;
            // ES6中三个点 即扩展运算符 指将数组转为参数数列(用逗号隔开的值)
            return {...state, ...action.data, redirectTo: getRedirectTo(header, type)};
        case ERROR_MSG: // data是msg
            return {...state, msg: action.data};
        case RECEIVE_USER:
            return action.data;
        case RESET_USER:
            return {...initUser, msg: action.data};
        default:
            return state
    }
}

export default combineReducers({
    user
});
// 向外暴露的状态结构{user:{}}

